package com.zxy.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zxy.demo.delay.DelayTaskManager;
import com.zxy.demo.delay.TaskIdGeneratorUtils;
import com.zxy.demo.delay.pojo.MyTask;
import com.zxy.demo.redis.RedisUtil;
import com.zxy.demo.spring.SpringContextUtils;

@RestController
public class TestController {
	
	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private TaskIdGeneratorUtils idUtils;
	
	@GetMapping("/hello")
	public String hello() {
		return "hello delay task demo";
	}
	
	@GetMapping("/test")
	public Object test (){
		StringBuffer sb = new StringBuffer();
		sb.append(redisUtil).append("获取bean成功").append((RedisUtil)SpringContextUtils.getBean("redisUtil"));
		// 下面开始测试
		// 01.通过静态方法获取任务管理器的实例
		DelayTaskManager taskManager = DelayTaskManager.getInstance();
		// 02.往任务管理器里面添加任务
		for (int i = 0; i < 20; i++) {
			MyTask myTask = new MyTask();
			myTask.setId(idUtils.generateLongId());
			myTask.setBeanName("redisUtil");
			myTask.setDelayTimeWithSecond((i+1) * 10);
			taskManager.add(myTask);
		}
		return sb.toString();
	}
}
